<?php

namespace App\Http\Controllers\Member;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;


class PasswordController extends Controller
{
    //
    public function create()
    {
        return view('member.password');
    }

    public function store(Request $request)
    {
        $user = Auth::user();


        $request->validate(
            ['old_password' => function ($attribute, $value, $fail)use($user) {
                if (!Hash::check($value, $user->password)) {
                        $fail('旧密码不正确');
                }
            },
                'password' => ['required', 'min:5', 'confirmed']
            ]);

        $user->password = $request->password;
        $user->save();
        return back()->with('success', '密码修改成功');
    }
}
